#include "../include/head.h"

void list_find(LinkList L)
{
    list_printf(L);
}
void entry_goods_success()
{
    print_border();
    printf("1.继续入库\n");
    printf("2.查看商品列表\n");
    printf("3.返回上一层菜单\n");
    print_border();
}
void list_set_next(LinkList L)
{
    char a;
    scanf(" %c", &a);
    switch (a)
    {
    case '1':
        list_set(L);
        break;
    case '2':
        printf("您选择了查看商品列表功能\n");
        list_printf(L);
        printf("1.返回上一层菜单\n");
        return_back(entry_goods_success);
        list_set_next(L);
        break;
    case '3':
        goods_manage();
        break;
    default:
        break;
    }
}
void list_set(LinkList L)
{
    goodsNode goods = userInput();
    entry_goods(L, goods);
    entry_goods_success();
    list_set_next(L);
}
// 修改商品
void list_change(LinkList L)
{
    printf("请输入要调整的商品名称\n");
    goodsNode goods;
    scanf("%s", goods.name);
    LinkList goodsNode = list_find_by_name(L, goods.name);
    if (goodsNode == NULL)
    {
        printf("没有找到该商品\n");
        goods_manage();
        return;
    }
    printf("请输入要调整的商品名称 价格 货架位置 数量\n");
    scanf(" %s %f %s %d", goods.name, &goods.price, goods.location, &goods.num);
    list_change_by_name(L, goods);
    // 更新文件
    write_data(L);
    printf("%s调整成功\n", goods.name);
}
// 出库商品
void list_out(LinkList L)
{
    printf("请输入要出库的商品名称 数量\n");
    char name[20];
    int num;
    scanf("%s %d", name, &num);
    // 根据名称查找
    list_reduce_num(L, name, num);
    LinkList goodsNode = list_find_by_name(L, name);
    // 查看出库的商品信息
    show_one_goods(goodsNode->data);
    // 更新商品文件
    write_data(L);
    // 更新销售记录文件
    write_sell_data(goodsNode->data);
}

// 删除商品
void list_del(LinkList L)
{
    printf("请输入要删除的商品名称\n");
    char name[20];
    scanf("%s", name);
    int delSucc = list_delte_by_name(L, name);
    if (delSucc)
    {
        printf("%s删除成功!\n", name);
        // 更新文件
        write_data(L);
        // list_printf(L);
    }
    else
    {
        printf("%s删除失败\n", name);
    }
    printf("1.返回上一层菜单\n");
    return_back(goods_manage);
}
// 接受用户输入
goodsNode userInput()
{
    printf("请输入商品信息(名称 价格 货架位置 数量)\n");
    goodsNode value;
    scanf("%s %f %s %d", value.name, &value.price, value.location, &value.num);
    return value;
}
// 写入注册账号记录
void write_user_data(char username[], char pass[])
{
    FILE *user_fp = fopen(STUDENTS_USER_FILE, "a+");
    fprintf(user_fp, "%s %s\n", username, pass);
    if (user_fp == NULL)
    {
        printf("打开文件失败\n");
        return;
    }
    fclose(user_fp);
}
// 修改账号密码
void write_user_pass()
{
    FILE *user_fp = fopen(STUDENTS_USER_FILE, "w+");

    if (user_fp == NULL)
    {
        printf("打开文件失败\n");
        return;
    }
    for (size_t i = 0; i < userNum; i++)
    {
        printf("%s,%s\n", userArr[i].userName, userArr[i].pass);
        fprintf(user_fp, "%s %s\n", userArr[i].userName, userArr[i].pass);
    }
    fclose(user_fp);
}
userData userArr[20];
int userNum = 0;
// 读取文件的账号记录
void read_user_data()
{
    // 读取文件的账号记录
    FILE *user_fp = fopen(STUDENTS_USER_FILE, "r");
    if (user_fp == NULL)
    {
        printf("打开文件失败\n");
    }
    char line[256]; // 假设每行数据不会超过255个字符

    int i = 0;
    while (fgets(line, sizeof(line), user_fp))
    {
        line[strcspn(line, "\n")] = 0;
        char userName[20];
        char pass[20];
        // 解析整行数据
        sscanf(line, "%s %s", userName, pass);
        strcpy(userArr[i].userName, userName);
        strcpy(userArr[i].pass, pass);
        i++;
    }
    userNum = i;
}
// 写入销售记录
void write_sell_data(goodsNode data)
{
    time_t currentTime;
    currentTime = time(NULL);
    struct tm *localTime;
    localTime = localtime(&currentTime);
    char dateTimeStr[100];
    strftime(dateTimeStr, sizeof(dateTimeStr), "%Y-%m-%d %H:%M:%S", localTime);
    FILE *sell_fp = fopen(STUDENTS_SELL_FILE, "a+");
    fprintf(sell_fp, "%s %f %s %d %s\n", data.name, data.price, data.location, data.num, dateTimeStr);
    if (sell_fp == NULL)
    {
        printf("打开文件失败\n");
        return;
    }
    fclose(sell_fp);
}

// 将销售记录的文件的内容读出来
void read_sell_data()
{
    FILE *sell_fp = fopen(STUDENTS_SELL_FILE, "r");
    if (sell_fp == NULL)
    {
        printf("打开文件失败\n");
    }
    char line[256]; // 假设每行数据不会超过255个字符
                    // 打印上边框
    for (size_t i = 0; i < 90; i++)
    {
        printf("=");
    }
    printf("\n");
    printf("名称\t\t\t价格\t\t货架位置\t\t数量\t\t售出时间\n");

    for (size_t i = 0; i < 90; i++)
    {
        printf("-");
    }
    printf("\n");
    while (fgets(line, sizeof(line), sell_fp))
    {
        line[strcspn(line, "\n")] = 0;
        char name[20];
        float price;
        int num;
        char location[20];
        char dateTimeStr[20];
        // 解析整行数据
        sscanf(line, "%s %f %s %d %s", name, &price, location, &num, dateTimeStr);
        // printf("%-10s\t%.2f\t\t%-15s\t\t%d\t\t%s\n", name, price, location, num, dateTimeStr);
        printf("%-25s", name);
        printf("%-17.2f", price);
        printf("%-28s", location);
        printf("%-14d", num);
        printf("%-16s", dateTimeStr);
        printf("\n");
    }
    for (size_t i = 0; i < 90; i++)
    {
        printf("=");
    }
    printf("\n");
    printf("1.返回上一层菜单\n");
}
// 整个将链表的数据写入到文件
int write_data(LinkList L)
{
    // FILE *fp = fopen(STUDENTS_FILE, "ab+");
    FILE *fp = fopen(STUDENTS_FILE, "w+");
    LinkList p = L->next;
    while (p)
    {
        fprintf(fp, "%s %f %s %d\n", p->data.name, p->data.price, p->data.location, p->data.num);
        p = p->next;
    }
    if (fp == NULL)
    {
        printf("打开文件失败\n");
        return -1;
    }
    // fwrite(&value, sizeof(goodsNode), 1, fp);
    fclose(fp);
    return 0;
}

// 将文件的内容读出来存到链表
goodsNode read_data(LinkList L)
{
    goodsNode goods;
    FILE *fp = fopen(STUDENTS_FILE, "r+");
    if (fp == NULL)
    {
        printf("打开文件失败\n");
    }
    char line[256]; // 假设每行数据不会超过255个字符
    while (fgets(line, sizeof(line), fp))
    {
        line[strcspn(line, "\n")] = 0;
        // 解析整行数据
        sscanf(line, "%s %f %s %d", goods.name, &goods.price, goods.location, &goods.num);
        list_tail_insert(L, goods);
    }
    return goods;
}

// 打印单个商品信息
void show_one_goods(goodsNode goods)
{
    for (size_t i = 0; i < 65; i++)
    {
        printf("*");
    }
    printf("\n");
    printf("+\t名称\t 价格\t\t 货架位置\t 数量\t\t+\n");
    printf("+\t%s\t%.2f\t\t%s\t\t%d\t\t+\n", goods.name, goods.price, goods.location, goods.num);
    for (size_t i = 0; i < 65; i++)
    {
        printf("*");
    }
    printf("\n");
}

void print_border()
{
    for (size_t i = 0; i < BORDER_WIDTH; i++)
    {
        printf("*");
    }
    printf("\n");
}

void print_empty_line()
{
    printf("*\t\t\t\t\t\t\t\t\t\t\t\t*\n");
}
// 首页菜单
void home_hint(LinkList L)
{
    char *menu[] = {
        "欢迎使用便利店管理系统",
        "1.商品管理",
        "2.库存管理",
        "3.销售管理",
        "4.退出",
    };
    print_border();
    for (int i = 0; i < sizeof(menu) / sizeof(menu[0]); i++)
    {
        printf("* %-*s   \n", 47, menu[i]);
    }
    print_border();
    int type = -1;
    scanf("%d", &type);
    switch (type)
    {
    case 1:
        printf("您选择了商品管理功能\n");
        goods_manage();
        break;
    case 2:
        printf("您选择了库存管理功能\n");
        repertory_manage();
        break;
    case 3:
        printf("您选择了销售功能\n");
        sell_manage();
        break;
    default:
        break;
    }
}

// 返回上一层
void return_back(void (*fun)(void))
{
    char a;
    scanf(" %c", &a);
    switch (a)
    {
    case '1':
        system("clear"); // linux
        system("cls");
        fun();
        break;

    default:
        break;
    }
}
// 商品管理菜单页
void goods_manage()
{
    char *menu[] = {
        "商品管理",
        "1.商品入库",
        "2.商品出库",
        "3.查找商品",
        "4.查看商品列表",
        "5.删除商品",
        "6.返回上一层菜单",
        "7.退出",
    };
    print_border();
    for (int i = 0; i < sizeof(menu) / sizeof(menu[0]); i++)
    {
        printf("* %-*s   \n", 46, menu[i]);
    }
    print_border();
    char type[10];
    scanf("%s", type);
    if (!strcmp(type, "6"))
    {
        home_hint(L);
    }
    else if (!strcmp(type, "1"))
    {
        printf("您选择了商品入库功能\n");
        list_set(L);
    }
    else if (!strcmp(type, "2"))
    {
        printf("您选择了商品出库功能\n");
        list_printf(L);
        list_out(L);
        printf("1.返回上一层菜单\n");
        return_back(goods_manage);
    }
    else if (!strcmp(type, "3"))
    {
        find_goods();
    }
    else if (!strcmp(type, "4"))
    {
        view_goods_list(goods_manage);
    }
    else if (!strcmp(type, "5"))
    {
        printf("您选择了删除商品功能\n");
        list_printf(L);
        list_del(L);
        printf("1.返回上一层菜单\n");
        return_back(goods_manage);
    }
}
// 查找商品
void find_goods()
{
    printf("请输入要查找的商品名称\n");
    goodsNode goods;
    scanf("%s", goods.name);
    LinkList goodsNode = list_find_by_name(L, goods.name);
    if (goodsNode == NULL)
    {
        printf("没有找到该商品\n");
    }
    else
    {
        show_one_goods(goodsNode->data);
    }
    printf("1.返回上一层菜单\n");
    return_back(goods_manage);
}
void view_goods_list(void (*fun)(void))
{
    printf("您选择了查看商品列表功能\n");
    list_printf(L);
    printf("1.返回上一层菜单\n");
    return_back(fun);
}
// 库存管理菜单页
void repertory_manage()
{
    char *menu[] = {
        "库存管理",
        "1.库存查询",
        "2.库存调整",
        "3.返回上一层菜单",
        "4.退出",
    };
    print_border();
    for (int i = 0; i < sizeof(menu) / sizeof(menu[0]); i++)
    {
        printf("* %-*s   \n", 46, menu[i]);
    }
    print_border();
    char type[10];
    scanf("%s", type);
    if (!strcmp(type, "3"))
    {
        home_hint(L);
    }
    else if (!strcmp(type, "1"))
    {
        printf("您选择了库存查询功能\n");
        list_printf(L);
        printf("1.返回上一层菜单\n");
        return_back(repertory_manage);
    }
    else if (!strcmp(type, "2"))
    {
        printf("您选择了库存调整功能\n");
        list_change(L);
        printf("1.返回上一层菜单\n");
        return_back(repertory_manage);
    }
}

// 销售管理菜单页
void sell_manage()
{
    char *menu[] = {
        "销售管理",
        "1.查看销售报表",
        "2.返回上一层菜单",
        "3.退出",
    };
    print_border();
    for (int i = 0; i < sizeof(menu) / sizeof(menu[0]); i++)
    {
        printf("* %-*s   \n", 46, menu[i]);
    }
    print_border();
    char type[10];
    scanf("%s", type);
    if (!strcmp(type, "2"))
    {
        home_hint(L);
    }
    else if (!strcmp(type, "1"))
    {
        printf("您选择了查看销售报表\n");
        read_sell_data();
        return_back(sell_manage);
    }
}
